######################################################################
# Time-stamp: <05/08/11 16:12:44 ostolop>
#
# Routines to validate BGA queries
######################################################################
package EP::EPC::Interface::Validator::Clustering_Comparison;
use strict;

use EP::Common;
use EP::Common::Error qw ( :try );

my $COMPID = 9;

sub new {
  my $proto = shift;
  my $class = ref($proto) || $proto;
  my $self  = {};
  bless $self, $class;
  return $self;
}

sub validate {
  my $self = shift;
  my ($component, $source) = @_;

  my $err_code = "";

  try {
    # old stuff.
#    check_cluster_files ( $component, $source );
  } catch EP::Common::Error with {
    my $e = shift;
    $err_code = "$e";
  };

  return $err_code;
}


sub check_cluster_files {
  my ($component, $source) = @_;
  my $err_code;

  my $dataset_id       = $component->find ('section[@id=0]/submit[@name=\'datasets___c_9_s_0\']/value')->pop()->textContent;
  my $clustering_file1 = $component->find ('section[@id=1]/submit[@name=\'clustering1___c_9_s_1\']/value')->pop()->textContent;
  my $clustering_file2 = $component->find ('section[@id=1]/submit[@name=\'clustering2___c_9_s_1\']/value')->pop()->textContent;

  my $data = { id => $dataset_id };
  my $dataset = EP::Common::DB::get_dataset ( $data );
  my $dataset_clusterings = EP::Common::DB::get_dataset_clusterings ($dataset->{0}{key});

  my $dataset_name = $dataset->{0}{value};
  my ($cf1_match, $cf2_match) = ( 0, 0 );

  foreach my $clustering ( @$dataset_clusterings ) {
    my $action_metadata = eval "my " . $clustering->[0];
    my $clustering_file = $action_metadata->{clustering};

    $cf1_match = 1 if $clustering_file1 eq $clustering_file;
    $cf2_match = 1 if $clustering_file2 eq $clustering_file;
  }

  if ( $cf1_match == 0 or $cf2_match == 0 ) {
    throw EP::Common::Error ( -value => CC_CLUSTERING_DS_NOT_CORRESPOND, -err_args => [ $dataset_name ] );
  }
}

1;
